<?php
	/**
	* Kiosk_model 
	* 
	* Kiosk_model manage the business methods for kiosk table 
	* 
	* 
	* @author Thang
	* @version $Id:  kiosk_model.php 2012-04-13
	* @access public
	*
	*/
	class Kiosk_model extends MY_Model{
	
		
		
		public function __construct(){
			parent::__construct();			
		}
				
		
		/**
		* getKioskList(): return data from kiosk table join location and fund tables, the action for it:
		* 					+ Load a list of kiosk with pagination		*					
		*					+ Load Kiosk to form
		* @param     int $id is the id of kiosk table
		* @return    array - records of KIOSK with limit record
		* @access public
		*/
		public function get_kiosk($id=FALSE){
			$this->db->select('k.*, l.address, SUM(f.donate) AS total_donate');
			$this->db->from('kiosk k');
			$this->db->join('location l', 'k.location_id = l.id', 'left');
			$this->db->join('fund f', 'k.id = f.kiosk_id', 'left');
			
			if($id!=FALSE){				
				$this->db->where('k.id = '.$id, NULL, FALSE);
				$this->db->group_by("k.id"); 
				$query = $this->db->get();	
				return $query->row_array();
			}		
			else{
				$this->db->group_by("k.id"); 
				$this->db->limit($this->record_per_page, $this->limit);
				$query = $this->db->get();	
				return $query->result_array();
			}
		}
		
		
		/**
		* get_kiosk_list(): return data from kiosk table join location and fund tables, the action for it:
		* 					load all record of kiosk to list box without LIMIT
		* @param    
		* @return    array - all records of KIOSK
		* @access public
		*/
		public function get_kiosk_list(){
			$this->db->select('k.*, l.address, SUM(f.donate) AS total_donate');
			$this->db->from('kiosk k');
			$this->db->join('location l', 'k.location_id = l.id', 'left');
			$this->db->join('fund f', 'k.id = f.kiosk_id', 'left');
			$this->db->group_by("k.id"); 
			$query = $this->db->get();	
			return $query->result_array();
		}
		
		
		/**
		* get_total(): 	return total of record of kiosk query
		* @param    
		* @return    int - total of record of KIOSK
		* @access public
		*/
		public function get_total(){
			return count($this->get_kiosk_list());
		}
		
		
		/**
		* search(): 	searching the KIOSK data
		* @param    	String $src is the search string
		* @return    	array - records of searching results
		* @access public
		*/
		public function search($src=FALSE){
			$this->db->select('k.*, l.address, SUM(f.donate) AS total_donate');
			$this->db->from('kiosk k');
			$this->db->join('location l', 'k.location_id = l.id', 'left');
			$this->db->join('fund f', 'k.id = f.kiosk_id', 'left');
			$this->db->like('k.name', $src);
			$this->db->or_like('k.mac_address', $src);
			$this->db->or_like('k.description', $src);
			$this->db->or_like('l.address', $src);
			$this->db->or_like('f.donate', $src);
			$this->db->or_like('f.date', $src);			
			$this->db->group_by("k.id");			
			$this->db->limit($this->record_per_page, $this->limit);
			$query = $this->db->get();				
			return $query->result_array();
		}
		
		
		/**
		* get_total_search(): 	count number of record in the searching query 
		* @param    String $src is the search string
		* @return    int - number of record of searching
		* @access public
		*/
		public function get_total_search($src=FALSE){
			$this->db->select('k.*, l.address, SUM(f.donate) AS total_donate');
			$this->db->from('kiosk k');
			$this->db->join('location l', 'k.location_id = l.id', 'left');
			$this->db->join('fund f', 'k.id = f.kiosk_id', 'left');
			$this->db->like('k.name', $src);
			$this->db->or_like('k.mac_address', $src);
			$this->db->or_like('k.description', $src);
			$this->db->or_like('l.address', $src);
			$this->db->or_like('f.donate', $src);
			$this->db->or_like('f.date', $src);			
			$this->db->group_by("k.id");
			$query = $this->db->get();				
			return $query->num_rows();
		}
		
		
		/**
		* get_kiosk_list_assign(): 	Get the list of KIOSK for assiged Ads - Textl
		*						
		* @param    	int $id is the id of KIOSK
		* @return    	Boolean or Array of KIOSK without the record with the ID parameter
		* @access public
		*/
		public function get_kiosk_list_assign($id=FALSE){
			if($id!=FALSE){
				$sql = "SELECT * FROM kiosk WHERE id NOT IN($id)";
				$query = $this->db->query($sql);
				return $query->result_array();
			}
			else return false;
		}
		
		
		/**
		* save(): Store the KIOSK data to database
		* 
		* @param
		* @return    
		* @access public
		*/
		public function save(){
			$id = $this->input->post('id');
			$password = trim($this->input->post('password'));
			$data = array(
				"mac_address" => $this->input->post('mac_address'),
				"name" => $this->input->post('name'),
				"location_id" => $this->input->post('location_id'),
				"description" => $this->input->post('description')
			);
			
			if(!empty($password))
				$data['password'] = MD5($password);
			
			if(empty($id))
				return $this->db->insert('kiosk', $data);
			else{
				$this->db->where('id', $id);
				return $this->db->update('kiosk', $data);
			}
		}
		
		
		/**
		* delete(): Delete the KIOSK record. Before deleting, check the reference at fund table
		* 
		* @param	: int $id
		* @return    
		* @access public
		*/
		public function delete($id){
			$sql = "SELECT `id` FROM `fund` WHERE `kiosk_id` = $id";
			$query = $this->db->query($sql);
			if($query->num_rows()==0){
				$this->db->where('id', $id);
				$this->db->delete('kiosk');
			}
		}
		
		
		/**
		* check_mac_address(): Check for duplicate MacAddress when add new or update.
		* 						with the updating task, do not get the record with id - parameter
		*
		* @param	: int $id - Id of KIOSK, String $mac - Mac address
		* @return    Boolean
		* @access public
		*/
		public function check_mac_address($id=FALSE, $mac){
			$this->db->select('*');
			$this->db->from('kiosk');
			$this->db->where('mac_address = "'.$mac.'"');
		
			if($id!=FALSE)
				$this->db->where('id <>'.$id);			
			$query = $this->db->get();
			if($query->num_rows()==0) return TRUE;
			else return FALSE;
		}
		
		
		/**
		* check_kiosk_name(): 	check for duplicate KIOSK name when add new or update.
		* 						with the updating task, do not get the record with id - parameter
		* 
		* @param	: int $id
		* @return    
		* @access public
		*/
		public function check_kiosk_name($id=FALSE, $name){
			$this->db->select('*');
			$this->db->from('kiosk');
			$this->db->where('name = "'.$name.'"');
		
			if($id!=FALSE)
				$this->db->where('id <>'.$id);			
			$query = $this->db->get();
			if($query->num_rows()==0) return TRUE;
			else return FALSE;
		}
		
		
		/**
		* get_total_kiosk(): get total kiosk 
		* 
		* @param     	
		* @return    number of kiosk devices
		* @access public
		*/
		public function get_total_kiosk(){
			$query = $this->db->get('kiosk');
			return $query->num_rows();
		}
		
			
		/**
		* login(): check login from kokiosk device using for API
		* 
		* @param     	String $username is the name of ekokiosk device
						String $password is the password if ekokiosk device
		* @return    true or false
		* @access public
		*/
		public function login($username, $password){
			$this->db->select("id, name, password");
			$this->db->from('kiosk');
			$this->db->where('name = '.$this->db->escape($username));
			$this->db->where('password = '.$this->db->escape(MD5($password)));
			
			$query = $this->db->get();
			if($query->num_rows()==1)
				return $query->row_array();
			else
				return false;
		}
	}
?>